{
    "cells": [
        {
            "cell_type": "code",
            "execution_count": 1,
            "source": [
                "import cv2\r\n",
                "import numpy as np"
            ],
            "outputs": [],
            "metadata": {}
        },
        {
            "cell_type": "code",
            "execution_count": 2,
            "source": [
                "cv2.__version__"
            ],
            "outputs": [
                {
                    "output_type": "execute_result",
                    "data": {
                        "text/plain": [
                            "'4.5.3'"
                        ]
                    },
                    "metadata": {},
                    "execution_count": 2
                }
            ],
            "metadata": {}
        },
        {
            "cell_type": "code",
            "execution_count": 8,
            "source": [
                "cap = cv2.VideoCapture(0)\r\n"
            ],
            "outputs": [],
            "metadata": {}
        },
        {
            "cell_type": "code",
            "execution_count": 4,
            "source": [
                "def ellipse_detect(img):\r\n",
                "    # img = cv2.imread(image,cv2.IMREAD_COLOR)\r\n",
                "    ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb) # 把图像转换到YUV色域\r\n",
                "    (y, cr, cb) = cv2.split(ycrcb) # 图像分割, 分别获取y, cr, br通道图像\r\n",
                "    # 高斯滤波, cr 是待滤波的源图像数据, (5,5)是值窗口大小, 0 是指根据窗口大小来计算高斯函数标准差\r\n",
                "    cr1 = cv2.GaussianBlur(cr, (5, 5), 0) # 对cr通道分量进行高斯滤波\r\n",
                "    # 根据OTSU算法求图像阈值, 对图像进行二值化\r\n",
                "    _, skin1 = cv2.threshold(cr1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) \r\n",
                "    return skin1\r\n"
            ],
            "outputs": [],
            "metadata": {}
        },
        {
            "cell_type": "code",
            "execution_count": 9,
            "source": [
                "for imgindex in range(100):\r\n",
                "    ret, frame = cap.read()\r\n",
                "    if ret == True:\r\n",
                "        res = ellipse_detect(frame)\r\n",
                "\r\n",
                "        # cv2.imshow('ori', frame)\r\n",
                "        # cv2.imshow('after', res)\r\n",
                "        img1_fg = cv2.bitwise_and(frame, frame, mask=res)\r\n",
                "        # cv2.imshow('after1', img1_fg)\r\n",
                "        cv2.imwrite('FIVE/'+str(imgindex)+'.jpg', img1_fg)\r\n",
                "    if cv2.waitKey(1) == ord('q'):\r\n",
                "        cv2.destroyAllWindows()\r\n",
                "        break\r\n",
                "    "
            ],
            "outputs": [],
            "metadata": {}
        },
        {
            "cell_type": "code",
            "execution_count": 6,
            "source": [
                "cap.release()"
            ],
            "outputs": [],
            "metadata": {}
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "source": [],
            "outputs": [],
            "metadata": {}
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "source": [],
            "outputs": [],
            "metadata": {}
        }
    ],
    "metadata": {
        "orig_nbformat": 4,
        "language_info": {
            "name": "python",
            "version": "3.7.11",
            "mimetype": "text/x-python",
            "codemirror_mode": {
                "name": "ipython",
                "version": 3
            },
            "pygments_lexer": "ipython3",
            "nbconvert_exporter": "python",
            "file_extension": ".py"
        },
        "kernelspec": {
            "name": "python3",
            "display_name": "Python 3.7.11 64-bit ('pytorch': conda)"
        },
        "interpreter": {
            "hash": "0b44c525ca95e5dbf893da2282eb3ec3f420cb9fa59d94f9af90ca833dc1a37c"
        }
    },
    "nbformat": 4,
    "nbformat_minor": 2
}